<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增案源委托')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-entrustment-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">事由：</label>
                <div class="col-sm-8">
                    <input name="subjectMatter" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">诉讼内容：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" name="litigationContent">
                    <div class="summernote" id="litigationContent"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">结束标准：</label>
                <div class="col-sm-8">
                    <input name="endCriteria" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">执行地区：</label>
                <div class="col-sm-8">
                    <input name="implementationArea" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">代理价格：</label>
                <div class="col-sm-8">
                    <input name="agencyPrice" class="form-control" type="number">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">有效时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="effectiveTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">管理编码：</label>
                <div class="col-sm-8">
                    <input name="managementCode" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">详细描述：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" name="detailedDescription">
                    <div class="summernote" id="detailedDescription"></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">附件：</label>
                <div class="col-sm-8">
                    <input type="hidden" id="fileName" name="fileName">
                    <input type="hidden" name="fileUrl">
                    <div class="file-loading">
                        <input class="file-upload" id="fileUrl" name="file" type="file">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "business/entrustment"
        $("#form-entrustment-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-entrustment-add').serialize());
            }
        }

        $("input[name='effectiveTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='approvalTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='payTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        var fileList = new Array();
        var fileName = new Array();
        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 1,
            autoReplace: true,
        }).on('fileuploaded', function (event, data, previewId, index) {
            fileList.push(data.response.url)
            fileName.push(data.response.name)
            $("input[name='" + event.currentTarget.id + "']").val(fileList)
            $("#fileName").val(fileName)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        }).on('filesuccessremove', function(event, id) {
            deleteArray(event, id);
        });
        function deleteArray(event, id){
            var name = id.split("_")[1];
            var index = fileName.indexOf(name);
            if(fileList.length > 0 && fileName.length > 0){
                fileList.splice(index,index);
                fileName.splice(index,index);
                $("input[name='" + event.currentTarget.id + "']").val(fileList)
                $("#fileName").val(fileName)
            }
        }
        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                height: 194,
                dialogsInBody: true,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    		    if (result.code == web_status.SUCCESS) {
                    		        $('#' + obj.id).summernote('insertImage', result.url);
                    		    } else {
                    		        $.modal.alertError(result.msg);
                    		    }
                    		},
                    		error: function(error) {
                    		    $.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
        });
    </script>
</body>
</html>